#!/bin/sh

# only clean load start this
if [ -f /tmp/nvram_checked ] || [ -f /var/run/goahead.pid ]; then
    exit 0
fi

LOG="echo CHECKCONF"

start() {
    # get variables
    get_param

    # include kernel config
    . /etc/scripts/config.sh

    echo "Check firmware update or clean boot."
    if [ "$IS_WIVE" = "YES" ]; then
	$LOG ">>>> NORMAL BOOT <<<<"
    else
	$LOG "Burn RF template to flash"
	fs burnrf
	$LOG ">>>> RESTORE NVRAM <<<<"
	    if [ -f /etc/backup/nvram_backup.dat ]; then
		$LOG "Load backuped setting to nvram."
    		fs load_nvram
	    else
		$LOG "Clear and load defaults to nvram."
    		fs nvramreset
	    fi
	    get_param
	    $LOG "Reset OK... Continue boot..."
    fi
    #
    # user and password need set
    #
    if [ "$Login" = "" ] || [ "$Password" = "" ]; then
        nvram_set 2860 Login "Admin"
        nvram_set 2860 Password "Admin"
    fi
    #
    # if check mac in nvram set to not NO and web not start
    #
    if [ "$CHECKMAC" != "NO" ] && [ ! -f /var/run/goahead.pid ]; then
	if [ $MAC_TEST = "0" ] || [ $WMAC_TEST = "0" ] || [ $LANMAC_TEST = "0" ]; then
	    $LOG "MAC is not $VENDNAME range - start MAC generate"
	    mac_wan=`cat /dev/urandom | hexdump | head -n1 | cut -f 2- -d " " | sed ':BEGIN;s/^\([^ ]\{0,29\}\) /\1/;tBEGIN' | sed 's/\(..\)/\1:/g; s/.$//' | tail -q -c 9`
	    mac_lan=`cat /dev/urandom | hexdump | head -n1 | cut -f 2- -d " " | sed ':BEGIN;s/^\([^ ]\{0,29\}\) /\1/;tBEGIN' | sed 's/\(..\)/\1:/g; s/.$//' | tail -q -c 9`
	    mac_wlan=`cat /dev/urandom | hexdump | head -n1 | cut -f 2- -d " " | sed ':BEGIN;s/^\([^ ]\{0,29\}\) /\1/;tBEGIN' | sed 's/\(..\)/\1:/g; s/.$//' | tail -q -c 9`
	    echo "Set new mac!"
	    MAC="$VENDMASK:$mac_wan"
	    LANMAC="$VENDMASK:$mac_lan"
	    WMAC="$VENDMASK:$mac_wlan"
    	    nvram_set 2860 WAN_MAC_ADDR  "$MAC"
    	    nvram_set 2860 LAN_MAC_ADDR  "$LANMAC"
    	    nvram_set 2860 WLAN_MAC_ADDR "$WMAC"
	fi
    fi
    #
    # OperationMode adjustment:
    #   if AP client was not compiled and operation mode was set "3" -> set $OperationMode "1"
    #   if Station was not compiled and operation mode was set "2" -> set $OperationMode "1"
    #   if OperationMode = "4" and chilli spot daemon not exist -> set $OperationMode "1"
    if   [ "$OperationMode" = "3" ] && [ "$CONFIG_RT2860V2_AP_APCLI" == "" ]; then
	nvram_set 2860 OperationMode 1
	OperationMode="1"
    elif [ "$OperationMode" = "2" ] && [ "$CONFIG_RT2860V2_STA" == "" ]; then
	nvram_set 2860 OperationMode 1
	OperationMode="1"
    elif [ "$OperationMode" = "4" ] && [ ! -f /bin/chilli ]; then
	nvram_set 2860 OperationMode 1
	OperationMode="1"
    fi
    if [ "$OperationMode" != "3" ]; then
	if [ "$ApCliBridgeOnly" != "0" ]; then
	    nvram_set ApCliBridgeOnly 0
	fi
	if [ "$ApCliClientOnly" != "0" ]; then
	    nvram_set ApCliClientOnly 0
	fi
    fi
    #
    # dafault lan ip/mask
    #
    if [ "$lan_ipaddr" = "" ] || [ "$lan_netmask" = "" ]; then
	nvram_set 2860 lan_ipaddr 192.168.1.1
	nvram_set 2860 lan_netmask 255.255.255.0
    fi

    #
    # need hostname
    #
    if [ "$HostName" = "" ]; then
	HostName="Wive-RTNL"
	nvram_set 2860 HostName "$HostName"
    fi
    echo "$HostName" > /etc/hostname
    hostname -F /etc/hostname
    #
    # samba configs to
    #
    if [ -f /bin/smbd ] || [ -f /bin/nmbd ]; then
	if [ "$WorkGroup" = "" ] || [ "$SmbNetBIOS" = "" ] || [ "$SmbString" = "" ]; then
	    WorkGroup="LOCALNET"
	    SmbNetBIOS="$HostName"
	    SmbString="$HostName-SERVER"
	    nvram_set 2860 WorkGroup LOCALNET
	    nvram_set 2860 SmbNetBIOS "$SmbNetBIOS"
	    nvram_set 2860 SmbString "$SmbString"
	fi
    fi
    #
    # Udpxy default port
    #
    if [ -f /bin/udpxy ] && [ "$UDPXYPort" = "" ]; then
	nvram_set 2860 UDPXYPort 81
    fi
    #
    # WebUI default port
    #
    if [ "$RemoteManagementPort" = "" ]; then
	nvram_set 2860 RemoteManagementPort 80
    fi

    # set checked flag
    touch /tmp/nvram_checked
}

get_param() {
    # set mask for macs generate and vendor name
    VENDMASK="00:0B:2B"
    VENDNAME="Acorp"
    # get parametrs
    eval `nvram_buf_get 2860 IS_WIVE CHECKMAC Login Password OperationMode lan_ipaddr lan_netmask \
	    WorkGroup SmbNetBIOS SmbString HostName UDPXYPort RemoteManagementPort ApCliBridgeOnly ApCliClientOnly`
    # test vendor macs range
    MAC_TEST=`nvram_get 2860 WAN_MAC_ADDR | grep "$VENDMASK" -c`
    WMAC_TEST=`nvram_get 2860 WLAN_MAC_ADDR | grep "$VENDMASK" -c`
    LANMAC_TEST=`nvram_get 2860 LAN_MAC_ADDR | grep "$VENDMASK" -c`
}

stop() {
    :
}

case "$1" in
	start)
	    start
	    ;;

	stop)
	    stop
	    ;;

	restart)
	    stop
	    start
	    ;;
	*)
	    echo $"Usage: $0 {start|stop|restart}"
	    exit 1
esac
